package com.ktjiaoyu.crm.controller;

import com.ktjiaoyu.crm.entity.Role;
import com.ktjiaoyu.crm.entity.User;
import com.ktjiaoyu.crm.service.IRoleService;
import com.ktjiaoyu.crm.service.IUserService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Resource
    private IUserService usrService;
    @Resource
    private IRoleService roleService;
    @RequestMapping(value = "/login")
    public String login(){
        return "login";
    }


    @RequestMapping(value = "/dologin")
    public String dologin(String usrName, String usrPassword, HttpServletRequest request){
        User user=usrService.login(usrName,usrPassword);
        if (user!=null){
            request.getSession().setAttribute("loginUser",user);
            return "main";
        }else {
            request.setAttribute("message","登录失败，用户名或账号密码错误");
            return "login";
        }
    }
    @RequestMapping(value = "/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");
        return "login";
    }
//    @RequestMapping(value = "/user/list")
//    public String findUsers(String usrName, Long roleId, Model model){
//        List<User> list=userService.findAllUsers();
//        model.addAttribute("users",list);
//        return "/user/list";
//    }

    @RequestMapping(value = "/user/list")
    public String list(Model model, String usrName,
                       @RequestParam(required = false,defaultValue = "0")Long roleId,
                       @RequestParam(required = false,defaultValue = "1")int pageIndex){
        Sort sort = Sort.by(Sort.Direction.ASC,"usrId");
        Pageable pageable= PageRequest.of(pageIndex-1,5,sort);
        Page<User> userPager=usrService.findUsers(usrName,roleId,pageable);
        model.addAttribute("userPager",userPager);
        model.addAttribute("usrName",usrName);
        model.addAttribute("roleId",roleId);
        List<Role> roles=roleService.findAllRoles();
        model.addAttribute("roles",roles);
        return "user/list";

    }

    @RequestMapping(value = "/user/add")
    public String add(Model model){
        List<Role>roles=roleService.findAllRoles();
        model.addAttribute("role",roles);
        return "user/add";
    }
    public String save(User user){
        usrService.saveUser(user);
        return "redirect:/user/list";
    }

    @RequestMapping(value = "/user/edit")
    public String edit(Long userId,Model model){
        User user=usrService.getUser(userId);
        List<Role>roles=roleService.findAllRoles();
        model.addAttribute("user",user);
        model.addAttribute("roles",roles);
        return "user/edit";
    }

    @RequestMapping(value = "/user/del")
    @ResponseBody
    public Map del(Long usrId){
        usrService.deleteUser(usrId);
        Map map=new HashMap();
        map.put("delResult","true");
        return map;
    }

    @RequestMapping(value = "/role/json")
    @ResponseBody
    public List<Role>findAllRoles(){
        List<Role>list=roleService.findAllRoles();
        return list;
    }
}
